package com.linewell.service.impl;

import com.linewell.dao.FocusMapper;
import com.linewell.entity.Article;
import com.linewell.entity.Focus;
import com.linewell.entity.Tag;
import com.linewell.entity.UserInfo;
import com.linewell.service.IFocusService;
import com.linewell.utils.Msg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Title: ssm2
 * @description:
 * @author: hyuhuang@linewell.com
 * @since:2018/1/18 16:44
 */
@Service
public class FocusService implements IFocusService {

    @Autowired
    FocusMapper focusDao;

    public Focus selectAllFocus(Long userId) {
        return focusDao.selectAllFocus(userId);
    }

    public Focus selectFive(long userId) {
        return focusDao.selectFive(userId);
    }

    public Msg attention(Long userId, Long focusId) {
        if (focusDao.selectAttention(userId,focusId).size() > 0){
            return Msg.fail().add("msg","您已经关注过了");
        } else if (focusDao.attention(userId,focusId)){
            UserInfo focusInfo = focusDao.selectFocus(userId);
            UserInfo fansInfo = focusDao.selectFans(focusId);
            focusDao.updateFocusAndFans(focusInfo.getFocusNum()+1,fansInfo.getFansNum()+1,userId,focusId);
            return Msg.success();
        } else {
            return Msg.fail().add("msg","未知原因,关注失败。");
        }
    }

    public Msg selectFocus(Long userId, Long focusId) {
        if (focusDao.selectAttention(userId,focusId).size()>0){
            return Msg.success();
        } else {
            return Msg.fail();
        }
    }

    public List<Tag> selectUserTag(Long userId) {
        return focusDao.selectUserTag(userId);
    }

    public Article recommendArticle(int tagId) {
        return focusDao.recommendArticle(tagId);
    }

    public Msg getUserTag(Long userId) {
        List<Tag> tagList = focusDao.selectUserTag(userId);
        return Msg.success().add("tagList",tagList);
    }

    public Msg getRecommendArticle(int tagId) {
        if (tagId <= 20){
            List<Article> article = focusDao.selectAllArticle(tagId);
            return Msg.success().add("article",article);
        } else {
            Article article = focusDao.recommendArticle(tagId);
            if (article == null) {
                return Msg.fail();
            }else {
                List<Article> list = new ArrayList<Article>();
                list.add(article);
                return Msg.success().add("article",list);
            }
        }

    }
}
